package com.llynosy.interview.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llynosy.interview.po.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 在service层封装通用的CRUD类
 * @param <T>
 */

public abstract class AbstractCommCRUD<T> {

    @Autowired
    protected Mapper<T> dao;


    public PageInfo<T> findPage(T t, int page, int size) {
        //分页
        PageHelper.startPage(page,size);
        //执行搜索
        return new PageInfo<T>(dao.select(t));
    }


    public PageInfo<T> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<T>(dao.selectAll());
    }


    public List<T> findList(T t) {
        //构建查询条件
        //Example example = Utils.commonCreateExample(t);
        //根据构建的条件查询数据
        return dao.select(t);
    }


    public void delete(Long id) {
        dao.deleteByPrimaryKey(id);
    }


    public void update(T t) {
        dao.updateByPrimaryKeySelective(t);
    }


    public void add(T t)
    {
        dao.insertSelective(t);
    }


    public T findById(Long id) {
        return dao.selectByPrimaryKey(id);
    }


    public List<T> findAll() {
        return dao.selectAll();
    }

}
